﻿using iService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Service;
using swiftCode.Bbs.Model;
using swiftCode.Bbs.Model.Model;

namespace swiftCode.Bbs.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ModuleController : ControllerBase
    {
        private readonly IModuleService _moduleService;
        private readonly IUserService _userService;

        public ModuleController(IModuleService moduleService, IUserService userService)
        {
            _moduleService = moduleService;
            _userService = userService;
        }


        /// <summary>
        /// 查看已有类别
        /// </summary>
        /// <returns></returns>
        [HttpGet(Name = "getAllModule")]
        public async Task<MessageModel<List<Module>>> getAll()
        {
            var module = await _moduleService.GetAllModules();
            return new MessageModel<List<Module>>()
            {
                success = module.Count == 0 ? false : true,
                msg = module.Count == 0 ? "未找到符合条件的内容" : "搜索成功",
                response = module.Count == 0 ? null : module
            };
        }


        /// <summary>
        /// 添加新类别
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="module"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpGet(Name = "addNewModule")]
        public async Task<MessageModel<String>> addNew(int userId, string ModuleName)
        {
            User user = await _userService.getSelfInfor(d => d.UserId == userId);
            Module existModule = await _moduleService.FindModule(d => d.ModuleName == ModuleName);
            if(user.Role == 2)
            {
                return new MessageModel<String>()
                {
                    success = false,
                    msg = "权限不足"
                };
            }
            else if(existModule != null)
            {
                return new MessageModel<String>()
                {
                    success = false,
                    msg = "已存在相同类型"
                };
            } 
            else
            {
                Module module = new Module();
                module.ModuleName = ModuleName;
                await _moduleService.AddNewModule(module, true);
                return new MessageModel<String>()
                {
                    success = true,
                    msg = "添加成功"
                };
            }
        }


        /// <summary>
        /// 删除类别
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="ModuleId"></param>
        /// <param name="confirm"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpGet(Name = "deleteModule")]
        public async Task<MessageModel<String>> deleteModule(int userId, int ModuleId)
        {
            User user = await _userService.getSelfInfor(d => d.UserId == userId);
            Module module = await _moduleService.GetModule(d => d.ModuleId == ModuleId);
            if (user.Role == 2)
            {
                return new MessageModel<String>()
                {
                    success = false,
                    msg = "权限不足"
                };
            }if(module == null)
            {
                return new MessageModel<String>()
                {
                    success = false,
                    msg = "该类型已经被删除"
                };
            }
            else
            {
                await _moduleService.DeleteModule(d => d.ModuleId == ModuleId, true);
                return new MessageModel<String>()
                {
                    success = true,
                    msg = "删除成功"
                };
            }  
        }


        /// <summary>
        /// 更新类型
        /// </summary>
        /// <param name="module"></param>
        /// <param name="confirm"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpGet(Name = "updateModule")]
        public async Task<MessageModel<String>> updateModule(int ModuleId, string ModuleName)
        {
            var exist = await _moduleService.FindAsyncNoTrack(d => d.ModuleId == ModuleId);
            Module module = await _moduleService.FindAsyncNoTrack(d => d.ModuleName == ModuleName);
            if(exist == null)
            {
                return new MessageModel<String>()
                {
                    success = true,
                    msg = "该类型已经被删除无法修改"
                };
            }
            if (module == null)
            {
                exist.ModuleName = ModuleName;
                await _moduleService.UpdateModule(exist, true);
                return new MessageModel<String>()
                {
                    success = true,
                    msg = "修改成功"
                };
            }
            else
            {
                return new MessageModel<String>()
                {
                    success = false,
                    msg = "已存在相同的类型"
                };
            }
        }
    }
}
